var isIE = !+"\v1"; 
var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    };
  }
};

var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
};

var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	};
};

var Each = function(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

var FileUpload = Class.create();
FileUpload.prototype = {
  initialize: function(form, folder, options) {
	this.Form = $(form);
	this.Form.method = "post";
	//注意ie的form没有enctype属性，要用encoding
	this.Form.encoding = "multipart/form-data";
	this.Folder = $(folder);
	this.Files = [];
	
	this.SetOptions(options);
	this.FileName = this.options.FileName;
	this.Limit = this.options.Limit;
	this.Distinct = !!this.options.Distinct;
	this.ExtIn = this.options.ExtIn;
	this.ExtOut = this.options.ExtOut;
	this.onIniFile = this.options.onIniFile;
	this.onEmpty = this.options.onEmpty;
	this.onNotExtIn = this.options.onNotExtIn;
	this.onExtOut = this.options.onExtOut;
	this.onLimite = this.options.onLimite;
	this.onSame = this.options.onSame;
	this.onFail = this.options.onFail;
	this.onIni = this.options.onIni;
	
	this.inputNo = 0;
	this.Init();
  },
  
  SetOptions: function(options) {
    this.options = {
		FileName:	"filename",
		onIniFile:	function(){},
		onEmpty:	function(){},
		Limit:		10,
		onLimite:	function(){},
		Distinct:	true,//是否不允许相同文件
		onSame:		function(){},//有相同文件时执行
		ExtIn:		[],//允许后缀名
		onNotExtIn:	function(){},//不是允许后缀名时执行
		ExtOut:		[],//禁止后缀名，当设置了ExtIn则ExtOut无效
		onExtOut:	function(){},//是禁止后缀名时执行
		onFail:		function(){},//文件不通过检测时执行(其中参数是file对象)
		onIni:		function(){}//重置时执行
    };
    Extend(this.options, options || {});
  },

  Init: function() {
	  this.Files = [];
	  Each(this.Folder.getElementsByTagName("input"), Bind(this, function(o){
			if(o.type == "file"){ o.value && this.Files.push(o); this.onIniFile(o); }
	  }));
	  var file = document.createElement("input");
	  this.inputNo++;
	  file.name = this.FileName + this.inputNo;
	  file.type = "file"; 
	  file.onchange = Bind(this, function(){ this.Check(file); this.Init(); });
	  this.Folder.appendChild(file);
	  this.onIni();
  },
  
  Check: function(file) {
	var bCheck = true;
	if(!file.value){
		bCheck = false; this.onEmpty();
	} else if(this.Limit && this.Files.length >= this.Limit){
		bCheck = false; this.onLimite();
	} else if(!!this.ExtIn.length && !RegExp("\.(" + this.ExtIn.join("|") + ")$", "i").test(file.value)){
		bCheck = false; this.onNotExtIn();
	} else if(!!this.ExtOut.length && RegExp("\.(" + this.ExtOut.join("|") + ")$", "i").test(file.value)) {
		bCheck = false; this.onExtOut();
	} else if(!!this.Distinct) {
		Each(this.Files, function(o){ if(o.value == file.value){ bCheck = false; } });
		if(!bCheck){ this.onSame(); }
	}
	!bCheck && this.onFail(file);
  },
  Delete: function(file) {
	this.Folder.removeChild(file);
	var newFiels = [];
	for(var i=0;i<this.Files.length;i++){
		if(this.Files[i].value != file.value){
			newFiels[i] = this.Files[i];
		}
	}
	this.Files = newFiels;
	$("uploadBt").disabled = this.Files.length <= 0;
	this.Ini();
  },
  Clear: function() {
	Each(this.Files, Bind(this, function(o){ this.Folder.removeChild(o); })); this.Ini();
  }
};

var fu = new FileUpload("fileform", "file_list" , { 
	onIniFile: function(file){ file.value ? file.style.display = "none" : this.Folder.removeChild(file); },
	onEmpty: function(){ showMessageDialog("请选择一个文件!"); },
	onLimite: function(){ showMessageDialog("超过上传限制!"); },
	onSame: function(){ showMessageDialog("已经有相同文件!"); },
	onNotExtIn:	function(){ showMessageDialog("只允许上传" + this.ExtIn.join("，") + "文件!"); },
	onFail: function(file){ this.Folder.removeChild(file); },
	onIni: function(){
		var arrRows = [];
		if(this.Files.length){
			var oThis = this;
			Each(this.Files, function(v){
				arrRows.push(v.value);
			});
		} 
		add2DsList(arrRows);
		$("uploadBt").disabled = this.Files.length <= 0;
	}
});

/*ds删除文件*/
function deleteFileFromDs(filename){
	for(var i=0;i<fu.Form.length;i++){
		var f = fu.Form[i];
		if(f.type == 'file' && f.value == filename){
			fu.Delete(f);
		}
	}
}

/*将文件放入ds行*/
function add2DsList(arrRows){
	var ds = window.$ds_attachment_ds;
	if(ds == null)
		return;
	
	var map = new HashMap();
	for(var k=0; k<ds.getRowCount(); k++){
		var row = ds.getRow(k);
		var file = row.getCellValue(ds.nameToIndex('filepath'));
		map.put(file,row);
	}
	for(var i=0; i<arrRows.length; i++){
		if(!map.containsKey(arrRows[i])){
			var newRow = ds.getEmptyRow();
			newRow.setCellValue(ds.nameToIndex('filepath'),arrRows[i]);
			ds.insertRow(ds.getRowCount(),newRow);
		}
	}
}

function showUploadLoading(){
	if(window.layForUploadLoading == null){
		window.layForUploadLoading = $ce("div");
		window.layForUploadLoading.style.background = "url(/lfw/frame/themes/images/transparent.gif)";
		window.layForUploadLoading.style.height = "100%";
		window.layForUploadLoading.style.width = "100%";
		window.layForUploadLoading.style.position = "absolute";
		window.layForUploadLoading.style.left = "0";
		window.layForUploadLoading.style.top = "0";
		window.layForUploadLoading.style.zIndex = "1000000000";
		window.layForUploadLoading.style.display = "none";
		//window.layForUploadLoading.style.border = "solid red 5px";
		window.layForUploadLoading.innerHTML = "<table width='100%' height='100%' cellspacing='0' cellpadding='0'><tr><td align='center'><img src='/lfw/frame/themes/images/loading.gif'/><div style='font-size:15pt;font-weight:bold;color:#94C6F9'>上传中、请稍候...</div><td></tr></table>";
	}
	window.layForUploadLoading.style.display = "block";
	document.body.appendChild(window.layForUploadLoading);
};

function hideUploadLoading(){
	if(window.layForUploadLoading != null)
		window.layForUploadLoading.style.display = "none";
};	
